package freeCellGuiGameMain;

import game.Game;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import freeCellBackend.Card;
import freeCellDataStructures.Cell;
import freeCellDataStructures.OrderedCascade;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class FreeCellGuiGameMain extends JPanel implements MouseListener {
	
	ArrayList<JLabel> JLabelcards;		//EACH CARD OBJECT IN GUI
	ArrayList<JLabel> JLabelcard_backs;	//EACH "PLACE-HOLDER OBJECT" IN GUI
	JLabel Selected1;				//THE CURRENTLY SELECTED CARD
	JLabel Selected2;
	JLabel temp;
	
	Game FCgame;					//HOLDS THE STATE OF THE GAME
	
	//GUI Information
	final int Window_H = 800;
    final int Window_W = 1250;
    final int Cascade_X = 50;
    final int Cascade_Y = 160;
    final int Cell_X = 10;
    final int Cell_Y = 10;
    final int Foundation_X = 510;
    final int Foundation_Y = 10;
    final int card_h = 96;
    final int card_w = 72;
    
    public FreeCellGuiGameMain() {
        setLayout(null);
        setBackground(Color.green);
        setSize(Window_W,Window_H);
        FCgame = new Game();
        init();
        setup();        
    }
    
    public void init(){ //INITIALIZE ALL CLICKABLE OBJECTS
        JLabelcards = new ArrayList<JLabel>();
        JLabelcard_backs = new ArrayList<JLabel>();
        try {
        	for(int i=1; i<53; i++) {
        		JLabelcards.add(new JLabel(new ImageIcon("images/cards/" + i + ".png")));
        		JLabelcards.get(i-1).addMouseListener(this);
        	}
        	for(int i=0; i<FCgame.cells.length+FCgame.foundations.length; i++) {
        		JLabelcard_backs.add(new JLabel(new ImageIcon("images/cards/b1fv.png")));
        		JLabelcard_backs.get(i).addMouseListener(this);
        	}
        	for(int i=0; i<FCgame.cascades.length; i++) {
        		JLabelcard_backs.add(new JLabel(new ImageIcon("images/cards/grn.png")));
        		JLabelcard_backs.get(i+FCgame.cells.length+FCgame.foundations.length).addMouseListener(this);
        	}
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void setup(){	//PLACE OBJECTS ACCORDING TO DATASTRUCTURES IN FCGAME
		//Start to place them
    	this.removeAll();
		List<Card> temp = null;
		Card test = null;
		//PLACE CARDS INTO CASCADES INTO JPANEL
		for(int i=0; i<FCgame.cascades.length; i++){
			temp = FCgame.cascades[i].getCascade();
			for(int j=(temp.size()-1); j>=0; j--){
				this.add(JLabelcards.get(temp.get(j).getIndex()));	//ADD JLABELCARDS IN PROPER POSITION.
				JLabelcards.get(temp.get(j).getIndex()).setBounds(Cascade_X + i*100, Cascade_Y + j*25, card_w, card_h);
			}
			this.add(JLabelcard_backs.get(FCgame.cells.length + FCgame.foundations.length + i));
			JLabelcard_backs.get(FCgame.cells.length + FCgame.foundations.length + i).setBounds(Cascade_X + i*100, Cascade_Y, card_w, card_h);
		}
		//PLACE CARDS INTO CELLS INTO JPANEL
		for(int i=0; i<FCgame.cells.length; i++){
			test = FCgame.cells[i].get();
			if(test != null){
				add(JLabelcards.get(test.getIndex()));
				JLabelcards.get(test.getIndex()).setBounds(Cell_X + i*100, Cell_Y, card_w, card_h);
			}else{
				add(JLabelcard_backs.get(i));
				JLabelcard_backs.get(i).setBounds(Cell_X + i*100, Cell_Y, card_w, card_h);
			}
		}
		//PLACE CARDS INTO FOUNDATIONS INTO JPANEL
		for(int i=0;i<FCgame.foundations.length;i++) {
			test = FCgame.foundations[i].get();
			if(test != null){
				add(JLabelcards.get(test.getIndex()));
				JLabelcards.get(test.getIndex()).setBounds(Foundation_X + i*100, Foundation_Y, card_w, card_h);
			}else{
				add(JLabelcard_backs.get(i+FCgame.cells.length));
				JLabelcard_backs.get(i+FCgame.cells.length).setBounds(Foundation_X + i*100, Foundation_Y, card_w, card_h);
			}
		}
    }
    
    private static void createAndShowGUI() { //CREATE A JFRAME AND ADD OUR JPANEL
        //Create and set up the window.
        JFrame frame = new JFrame("Freecell By: Patrick Blesi and Joe Stacko");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1250,800);

        //Add content to the window.
        frame.add(new FreeCellGuiGameMain());

        //Display the window.
        frame.setVisible(true);
    }

	public static void main(String[] args) { //EXECUTE GUI
		//creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
	        UIManager.put("swing.boldMetal", Boolean.FALSE);	//Turn off metal's use of bold fonts
	        createAndShowGUI();
            }
        });
		}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseReleased(MouseEvent e) {
		
		int S1LocationInCascade;
		int S1LocationInCell;
		//NEVER USED: int S1LocationInFoundation;
		
		int S2LocationInCascade;
		int S2LocationInCell;
		int S2LocationInFoundation;
		
		temp = (JLabel) e.getComponent();
		
		if(Selected1 == null){ //ASSIGNS FIRST CARD
			if(JLabelcards.indexOf(temp) != -1 && FCgame.locationOfCardInFoundation(new Card(JLabelcards.indexOf(temp))) == -1) {//ONLY SELECT CARDS
				Selected1 = temp;
				Selected1.setIcon(new ImageIcon("images/cards/" + (JLabelcards.indexOf(Selected1)+1) + "h.png"));
			} 
		}else if(Selected1==temp) {
			Selected1.setIcon(new ImageIcon("images/cards/" + (JLabelcards.indexOf(Selected1)+1) + ".png"));
			Selected1 = null;
		}else{	//IF A CARD "IS" SELECTED ALREADY...
			Selected2 = temp;
			
			S1LocationInCascade = FCgame.locationOfCardInCascade(new Card(JLabelcards.indexOf(Selected1)));
			S1LocationInCell = FCgame.locationOfCardInCell(new Card(JLabelcards.indexOf(Selected1)));
			
			S2LocationInCascade = FCgame.locationOfCardInCascade(new Card(JLabelcards.indexOf(Selected2)));
			S2LocationInCell = FCgame.locationOfCardInCell(new Card(JLabelcards.indexOf(Selected2)));
			S2LocationInFoundation = FCgame.locationOfCardInFoundation(new Card(JLabelcards.indexOf(Selected2)));
			
			//HANDLE SPECIAL CASES I.E. OBJECT IS NOT CARD, BUT SOME EMPTY LOCATION(CELL, CASCADE, FOUNDATION);
			if (S2LocationInCascade == -1 && S2LocationInCell == -1 && S2LocationInFoundation == -1) {
				int tempLocation = JLabelcard_backs.indexOf(Selected2);
				if (tempLocation >= 0 && tempLocation < FCgame.cells.length) {
					S2LocationInCell = tempLocation;
				} else if (tempLocation >= FCgame.cells.length && tempLocation < (FCgame.cells.length + FCgame.foundations.length)) {
					S2LocationInFoundation = tempLocation - FCgame.cells.length;
				} else {
					S2LocationInCascade = tempLocation - FCgame.cells.length - FCgame.foundations.length;
				}
			}
			
			//ACTUALLY MOVE ELEMENTS
			if (S1LocationInCascade > -1 && S2LocationInCascade > -1) {
				//JOptionPane.showMessageDialog(null, "Cascade1 = " + S1LocationInCascade + "Cascade2 = " + S2LocationInCascade);
				if(FCgame.moveOCToCascade(S1LocationInCascade, S2LocationInCascade)) {
					Selected1.setIcon(new ImageIcon("images/cards/" + (JLabelcards.indexOf(Selected1)+1) + ".png"));
					Selected1 = null;
				}
				
			} else if (S1LocationInCascade > -1 && S2LocationInCell > -1) {
				//JOptionPane.showMessageDialog(null, "Cascade1 = " + S1LocationInCascade + "Cell2 = " + S2LocationInCell);
				if(FCgame.moveCardToCell(S1LocationInCascade, S2LocationInCell)) {
					Selected1.setIcon(new ImageIcon("images/cards/" + (JLabelcards.indexOf(Selected1)+1) + ".png"));
					Selected1 = null;
				}
			} else if (S1LocationInCascade > -1 && S2LocationInFoundation > -1) {
				//JOptionPane.showMessageDialog(null, "Cascade1 = " + S1LocationInCascade + "Foundation2 = " + S2LocationInFoundation);
				if(FCgame.moveCardToFoundation(S1LocationInCascade, S2LocationInFoundation)) {
					Selected1.setIcon(new ImageIcon("images/cards/" + (JLabelcards.indexOf(Selected1)+1) + ".png"));
					Selected1 = null;
				}
			} else if (S1LocationInCell > -1 && S2LocationInCascade > -1) {
				//JOptionPane.showMessageDialog(null, "Cell1 = " + S1LocationInCell + "Cascade2 = " + S2LocationInCascade);
				if(FCgame.moveCellToCascade(S1LocationInCell, S2LocationInCascade)) {
					Selected1.setIcon(new ImageIcon("images/cards/" + (JLabelcards.indexOf(Selected1)+1) + ".png"));
					Selected1 = null;
				}
			} else if (S1LocationInCell > -1 && S2LocationInCell > -1) {
				//JOptionPane.showMessageDialog(null, "Cell1 = " + S1LocationInCell + "Cell2 = " + S2LocationInCell);
				if(FCgame.moveCellToCell(S1LocationInCell, S2LocationInCell)) {
					Selected1.setIcon(new ImageIcon("images/cards/" + (JLabelcards.indexOf(Selected1)+1) + ".png"));
					Selected1 = null;
				}
			} else if (S1LocationInCell > -1 && S2LocationInFoundation > -1) {
				//JOptionPane.showMessageDialog(null, "Cell1 = " + S1LocationInCell + "Foundation2 = " + S2LocationInFoundation);
				if(FCgame.moveCellToFoundation(S1LocationInCell, S2LocationInFoundation)) {
					Selected1.setIcon(new ImageIcon("images/cards/" + (JLabelcards.indexOf(Selected1)+1) + ".png"));
					Selected1 = null;
				}
			}
			Selected2 = null;
		}
		//JOptionPane.showMessageDialog(null, "Now here");
		setup();
		}

}
